
// 引入axios
const axios = require('axios')
// // 导出模块对象
module.exports = {
	// 获取预约审核列表
    get_order_list: async (ctx, next) => {
        const {status} = ctx.request.body
        const {startPage,endPage} = await ctx.service.utils.paging(ctx)
        await ctx.db.query('select * from yuyueorder where status=?',[status]).then(async res=>{
            const total =  res[0].length
            await ctx.db.query('select * from yuyueorder where status=? order by createTime desc limit ?,?',[status,startPage,endPage]).then(async ress=>{
                ctx.body = {
                    code:200,
                    msg:'获取成功',
                    orderList:ress[0],
                    total:total,
                }
            }).catch(err=>{
                ctx.body = {
                    code:300,
                    msg:'获取失败'+err,
                }
            })
        })
    },
    // 根据id搜索自己的预约
    search_id_order: async (ctx, next) => {
        const {id} = ctx.request.body
        const {startPage,endPage} = await ctx.service.utils.paging(ctx)
        try {
            let [updateRes] = await ctx.db.query(`select * from yuyueorder where userId=? order by createTime desc limit ?,?`, [id,startPage,endPage])
             if(updateRes.length >=1){
                return ctx.body = {
                    code:200,
                    msg:'搜索成功',
                    orderList:updateRes
                }
            }else{
                return ctx.body = {
                    code:200,
                    msg:'没有搜索到'
                }
            }
        } catch (error) {
            return ctx.body = {
                code:500,
                msg:'搜索失败'+error
            }
        }
    },
    // 通过（未通过）审核status=>0待审核，1审核通过,2未通过，3接受预约，4拒绝预约,5完成订单，6取消预约
    set_order_status: async (ctx, next) => {
        const {id,status,note} = ctx.request.body
        if(status == 2){
            
            try {
                let [updateRes] = await ctx.db.query('update yuyueorder set status=?,note=?,updateTime=? where id=?', [status,note,new Date(),id])
                if(updateRes.affectedRows >= 1){
                    return ctx.body = {
                        code:200,
                        msg:'修改预约成功'
                    }
                }else{
                    return ctx.body = {
                        code:300,
                        msg:'修改预约失败'
                    }
                }
            } catch (error) {
                return ctx.body = {
                    code:500,
                    msg:'修改预约失败'+error
                }
        }
        }else{
            try {
                let [updateRes] = await ctx.db.query('update yuyueorder set status=?,updateTime=? where id=?', [status,new Date(),id])
                if(updateRes.affectedRows >= 1){
                    return ctx.body = {
                        code:200,
                        msg:'修改预约成功'
                    }
                }else{
                    return ctx.body = {
                        code:300,
                        msg:'修改预约失败'
                    }
                }
            } catch (error) {
                return ctx.body = {
                    code:500,
                    msg:'修改预约失败'+error
                }
        }
        }
      
    },
    // 发起预约
    add_order: async (ctx, next) => {
        const {userId,userPhone,xinliId,xinliPhone,adress,username,xinliname} = ctx.request.body
        console.log(userId,userPhone,xinliId,xinliPhone,adress,username,xinliname);
        
            try {
                let [updateRes] = await ctx.db.query('insert into yuyueorder(userId,userPhone,xinliId,xinliPhone,adress,createTime,username,xinliname) values(?,?,?,?,?,?,?,?)', [userId,userPhone,xinliId,xinliPhone,adress,new Date(),username,xinliname])
                if(updateRes.affectedRows >=1){
                    return ctx.body = {
                        code:200,
                        msg:'预约成功'
                    }
                }else{
                    return ctx.body = {
                        code:300,
                        msg:'预约失败'
                    }
                }
            } catch (error) {
                return ctx.body = {
                    code:500,
                    msg:'预约失败'+error
                }
            }
    },
}